package com.scraper;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * This class is responsible for invoking the WebScraper class services. 
 * 
 * @author Micah Perry
 */
public class WebReader {
	String targetUrl;
	Document targetDocument;
	WebScraper scraper; 

	/**
	 * This constructor sets the WebScraper member variable. 
	 * 
	 * @param specificScraper Instance of a specialized WebScraper class. 
	 */
	public WebReader(WebScraper specificScraper){
		setScraper(specificScraper); 
	}
	
	/**
	 * This method retrieves Document representation of the HTML of a particular webpage.
	 * 
	 * @param inputURL The URL of the webpage to be scraped.
	 * @return The Document representing the HTML to be scraped. 
	 */
	Document getTargetDocument(String inputURL){
		Document returnDocument = null; 
		
		// TODO: Properly handle exception
		try {
			returnDocument = Jsoup.connect(inputURL).get();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		targetDocument = returnDocument; 
		
		return returnDocument;
	}
	
	/**
	 * This method invokes the services of WebScraper.
	 */
	public void scrapeWebPage(String inputURL){
		getTargetDocument(inputURL); 
		
		// TODO: Possibly move internal scrapeWebPage functionality to this method to coincide more closely to the builder design pattern
		scraper.scrapeWebPage(this.targetDocument); 
	}
	
	/**
	 * This method sets the WebScraper member variable. 
	 * 
	 * @param specificScraper Instance of a specialized WebScraper class.
	 */
	public void setScraper(WebScraper specificScraper){
		scraper = specificScraper; 
	}
}
